package com.one.group.admin.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.processor.util.StrUtil;
import com.one.group.admin.intercepter.AdminThreadLocal;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.UpdateVendorDto;
import com.one.group.model.dto.VendorQueryDto;
import com.one.group.model.entity.Vendor;
import com.one.group.result.Result;
import com.one.group.service.VendorService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("vendor")
@Tag(name = "供应商管理接口")
public class VendorController {
    @Autowired
    VendorService vendorService;
    @PostMapping("list")
    @Operation(summary = "查询供应商列表")
    public Result list(@RequestBody VendorQueryDto vendorQueryDto){
//        1.准备分页条件
        Page<Vendor> page = new Page<>(vendorQueryDto.getPage(), vendorQueryDto.getLimit());
//        2.链式编程准备查询条件
        QueryWrapper queryWrapper = new QueryWrapper();

// 2. 构建查询条件
        String name = vendorQueryDto.getName();
        if (!StrUtil.isBlank(name)) {
            queryWrapper.where(
                    QueryMethods.column(Vendor::getName).like(name)
                            .or(QueryMethods.column(Vendor::getPerson).like(name))
            );
        }
//        3.查询，返回分页数据
        Page<Vendor> vendorPage = vendorService.page(page, queryWrapper);
        return ResultUtil.success(vendorPage);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询供应商信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(vendorService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有供应商信息")
    public Result findAll() {
        return ResultUtil.success(vendorService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除供应商信息")
    public Result delete(@RequestParam Long id){
        if (vendorService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加供应商信息")
    public Result add(@RequestBody UpdateVendorDto updateVendorDto){
        Vendor vendor = new Vendor();
        BeanUtils.copyProperties(updateVendorDto,vendor);
        if (vendorService.save(vendor)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改供应商信息")
    public Result update(@RequestBody UpdateVendorDto updateVendorDto){
        Vendor vendor = new Vendor();
        BeanUtils.copyProperties(updateVendorDto,vendor);
        if (vendorService.updateById(vendor)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除供应商信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (vendorService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
